package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.IProductCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author dinghongrui
 * @create 2022/09/21 0:07
 */
@Service
public class IProductCateServiceImpl implements IProductCateService {

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ProductCateConverter productCateConverter;


    @Override
    public List<ProductCateDto> getAllProductCate(String sort) {

        Example example = new Example(ItemCat.class);

        if (sort == null) {
            example.setOrderByClause("sort_order asc");
        } else {
            example.setOrderByClause("sort_order" + " " + sort);
        }
        List<ItemCat> itemCats = itemCatMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(itemCats)) {
            return null;
        }

        List<ProductCateDto> productCateDtos = productCateConverter.items2Dto(itemCats);
        return productCateDtos;
    }
}
